package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.dao.RoleDao;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.pojo.Menu;
import com.itheima.pojo.Permission;
import com.itheima.pojo.Role;
import com.itheima.service.MenuService;
import com.itheima.service.PermissionService;
import com.itheima.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Service(interfaceClass = RoleService.class)
@Transactional
public class RoleServiceImpl implements RoleService {

    @Autowired//角色
    private RoleDao roleDao;
    @Autowired   //菜单
    private MenuService menuService;

    @Autowired //权限
    private PermissionService permissionService;


    @Override
    public void add(Role role, Integer[] permissions, Integer[] menus) {
        //先添加role角色
        roleDao.add(role);
        Integer roleId = role.getId();
        this.setRoleAndPermissionsAndMenus(roleId, permissions, menus);

    }

    @Override
    public PageResult findPage(QueryPageBean queryPageBean) {

        Integer pageSize = queryPageBean.getPageSize();
        Integer currentPage = queryPageBean.getCurrentPage();
        String queryString = queryPageBean.getQueryString();
        PageHelper.startPage(currentPage, pageSize);
        Page<Role> page = roleDao.findPage(queryString);
        return new PageResult(page.getTotal(), page.getResult());
    }

    @Override
    public Map findAll() {
        Map<String, Object> map = new HashMap<>();
        List<Menu> menus = menuService.findAll();
        List<Permission> permissions = permissionService.findAll();
        map.put("menus", menus);
        map.put("permissions", permissions);
        return map;
    }

    @Override
    public void edit(Role role, Integer[] permissions, Integer[] menus) {
        roleDao.edit(role);
        roleDao.deleteRoleAndMenus(role.getId());
        roleDao.deleteRoleAndPerssions(role.getId());
        Integer roleId = role.getId();
        this.setRoleAndPermissionsAndMenus(roleId, permissions, menus);
    }

    @Override
    public Map findById(Integer id) {
        Role formData = roleDao.findById(id);
        //查中间表
        List<Integer> menus= roleDao.findRoleAndMenus(id);
        //查中间表
        List<Integer> permissions = roleDao.findRoleAndPermssions(id);
        List<Permission> tableData = permissionService.findAll();
        List<Menu> third = menuService.findAll();
        Map<String, Object> map = new HashMap<>();
        map.put("formData", formData);
        map.put("tableData",tableData);
        map.put("thirdData",third);
        map.put("permissions",permissions);
        map.put("menus",menus);
        return map;
    }

    @Override
    public void delete(Integer id) {
        List<Integer> roleAndMenus = roleDao.findRoleAndMenus(id);
        List<Integer> roleAndPermssions = roleDao.findRoleAndPermssions(id);
        if (roleAndMenus.size()>0&&roleAndPermssions.size()>0){
            throw  new RuntimeException("该表有中间表不能删除");
        }else{
            roleDao.deleteRole(id);
        }
    }


    public void setRoleAndPermissionsAndMenus(Integer id, Integer[] permissions, Integer[] menus) {
        //根据表关系处理 role- permssion  role-menu 两个中间表  将一个表拆成两份
        int permissionLength = permissions.length;
        int menusLength = menus.length;
        if (id != null && permissionLength > 0) {
            for (Integer permissionId : permissions) {
                Map<String, Integer> map = new HashMap<>();
                map.put("roleId", id);
                map.put("permissionId", permissionId);
                roleDao.addRoeAndPermssion(map);
            }
        }
        if (id != null && menusLength > 0) {
            for (Integer menuId : menus) {
                Map<String, Integer> map = new HashMap<>();
                map.put("menuId", menuId);
                map.put("roleId", id);
                roleDao.addRoleAndMenu(map);
            }
        }


    }
}
